package com.lenovo.newsapp.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lenovo.newsapp.R;
import com.lenovo.newsapp.model.HomeNews;
import com.lenovo.newsapp.util.Const;
import com.lenovo.newsapp.util.Tools;

public class GalleriesListAdapter extends BaseArrayAdapter<HomeNews> {

	private static final int FRIST = 1;
	private static final int SECOND = 2;
	private static final int THIRD = 3;
	private static final int ZERO = 0;
	private ViewHolder holder;
	private boolean isLoadDB;

	public GalleriesListAdapter(Context context, ArrayList<HomeNews> list) {
		super(context, list, null);
	}

	public boolean isLoadDB(boolean isLoadDB) {
		this.isLoadDB = isLoadDB;
		return isLoadDB;
	}

	@Override
	public int getItemViewType(int position) {
		if (position % 4 == 0) {
			return ZERO;
		} else if (position % 4 == 1) {
			return FRIST;
		} else if (position % 4 == 2) {
			return SECOND;
		} else {
			return THIRD;
		}

	};

	@Override
	public int getViewTypeCount() {
		return 4;
	};

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.galleries_list_item, null);
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.imgCount = (TextView) convertView
					.findViewById(R.id.img_count);
			holder.commentCount = (TextView) convertView
					.findViewById(R.id.comment_count);
			holder.imgLayoutOnly = (LinearLayout) convertView
					.findViewById(R.id.img_layout_only);
			holder.imgLayoutRight = (LinearLayout) convertView
					.findViewById(R.id.img_layout_right);
			holder.imgLayoutLeft = (LinearLayout) convertView
					.findViewById(R.id.img_layout_left);
			holder.onlyImg = (ImageView) convertView
					.findViewById(R.id.img_only);
			holder.rightLeftImg = (ImageView) convertView
					.findViewById(R.id.img_right_left);
			holder.rightTopImg = (ImageView) convertView
					.findViewById(R.id.img_right_top);
			holder.rightBottomImg = (ImageView) convertView
					.findViewById(R.id.img_right_bottom);
			holder.leftRightImg = (ImageView) convertView
					.findViewById(R.id.img_left_right);
			holder.leftTopImg = (ImageView) convertView
					.findViewById(R.id.img_left_top);
			holder.leftBottomImg = (ImageView) convertView
					.findViewById(R.id.img_left_bottom);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		HomeNews item = (HomeNews) getItem(position);
		Log.v(Const.DEBUG, "position--" + position);
		holder.title.setText(item.getTitle());
//		holder.imgCount.setText(item.getImgSize());
		// holder.commentCount.setText(item.getTitle());
		if (getItemViewType(position) == ZERO
				|| getItemViewType(position) == SECOND) {
			holder.imgLayoutOnly.setVisibility(View.VISIBLE);
			holder.imgLayoutRight.setVisibility(View.GONE);
			holder.imgLayoutLeft.setVisibility(View.GONE);

			if (item.getImgFrist() != null && !item.getImgFrist().equals("")) {
				holder.onlyImg.setVisibility(View.VISIBLE);

				final String url = Const.DOMAIN + item.getImgFrist();
				Tools.setImg(url, holder.onlyImg, context);
			} else {
				holder.onlyImg.setVisibility(View.GONE);
			}

		} else if (getItemViewType(position) == FRIST) {
			holder.imgLayoutOnly.setVisibility(View.GONE);
			holder.imgLayoutRight.setVisibility(View.VISIBLE);
			holder.imgLayoutLeft.setVisibility(View.GONE);
			if (item.getImgFrist() != null && !item.getImgFrist().equals("")) {
				holder.rightLeftImg.setVisibility(View.VISIBLE);

				final String url = Const.DOMAIN + item.getImgFrist();
				Tools.setImg(url, holder.rightLeftImg, context);
			} else {
				holder.rightLeftImg.setVisibility(View.GONE);
			}

			if (item.getImgSecend() != null && !item.getImgSecend().equals("")) {
				holder.rightTopImg.setVisibility(View.VISIBLE);

				final String url = Const.DOMAIN + item.getImgSecend();
				Tools.setImg(url, holder.rightTopImg, context);

			} else {
				holder.rightTopImg.setVisibility(View.GONE);
			}
			if (item.getImgThird() != null && !item.getImgThird().equals("")) {
				holder.rightBottomImg.setVisibility(View.VISIBLE);

				final String url = Const.DOMAIN + item.getImgThird();
				Tools.setImg(url, holder.rightBottomImg, context);

			} else {
				holder.rightBottomImg.setVisibility(View.GONE);
			}

		} else if (getItemViewType(position) == THIRD) {
			holder.imgLayoutOnly.setVisibility(View.GONE);
			holder.imgLayoutRight.setVisibility(View.GONE);
			holder.imgLayoutLeft.setVisibility(View.VISIBLE);
			if (item.getImgFrist() != null && !item.getImgFrist().equals("")) {
				holder.leftRightImg.setVisibility(View.VISIBLE);

				final String url = Const.DOMAIN + item.getImgFrist();
				Tools.setImg(url, holder.leftRightImg, context);
			} else {
				holder.leftRightImg.setVisibility(View.GONE);
			}

			if (item.getImgSecend() != null && !item.getImgSecend().equals("")) {
				holder.leftTopImg.setVisibility(View.VISIBLE);
				final String url = Const.DOMAIN + item.getImgSecend();
				Tools.setImg(url, holder.leftTopImg, context);
			} else {
				holder.leftTopImg.setVisibility(View.GONE);
			}
			if (item.getImgThird() != null && !item.getImgThird().equals("")) {
				holder.leftBottomImg.setVisibility(View.VISIBLE);
				final String url = Const.DOMAIN + item.getImgThird();
				Tools.setImg(url, holder.leftBottomImg, context);
			} else {
				holder.leftBottomImg.setVisibility(View.GONE);
			}

		}
		// if ( getItemViewType(position) == ZERO || getItemViewType(position)
		// == SECOND) {
		// holder.imgLayoutOnly.setVisibility(View.VISIBLE);
		// holder.imgLayoutRight.setVisibility(View.GONE);
		// holder.imgLayoutLeft.setVisibility(View.GONE);
		//
		// if (item.getImgFrist() != null && !item.getImgFrist().equals("")) {
		// holder.onlyImg.setVisibility(View.VISIBLE);
		// if(isLoadDB){
		// Tools.setDBImg(item.getImgFrist(), holder.onlyImg);
		// }else{
		//
		// final String url = Const.DOMAIN + item.getImgFrist();
		// Tools.setImg(url, holder.onlyImg, context);
		// }
		// } else {
		// holder.onlyImg.setVisibility(View.GONE);
		// }
		//
		// } else if ( getItemViewType(position) == FRIST){
		// holder.imgLayoutOnly.setVisibility(View.GONE);
		// holder.imgLayoutRight.setVisibility(View.VISIBLE);
		// holder.imgLayoutLeft.setVisibility(View.GONE);
		// if (item.getImgFrist() != null && !item.getImgFrist().equals("")) {
		// holder.rightLeftImg.setVisibility(View.VISIBLE);
		// if(isLoadDB){
		// Tools.setDBImg(item.getImgFrist(), holder.rightLeftImg);
		// }else{
		//
		// final String url = Const.DOMAIN + item.getImgFrist();
		// Tools.setImg(url, holder.rightLeftImg, context);
		// }
		// } else {
		// holder.rightLeftImg.setVisibility(View.GONE);
		// }
		//
		// if (item.getImgSecend() != null && !item.getImgSecend().equals("")) {
		// holder.rightTopImg.setVisibility(View.VISIBLE);
		// if(isLoadDB){
		// Tools.setDBImg(item.getImgSecend(), holder.rightTopImg);
		// }else{
		//
		// final String url = Const.DOMAIN + item.getImgSecend();
		// Tools.setImg(url, holder.rightTopImg, context);
		// }
		//
		// } else {
		// holder.rightTopImg.setVisibility(View.GONE);
		// }
		// if (item.getImgThird() != null && !item.getImgThird().equals("")) {
		// holder.rightBottomImg.setVisibility(View.VISIBLE);
		// if(isLoadDB){
		// Tools.setDBImg(item.getImgThird(), holder.rightBottomImg);
		// }else{
		//
		// final String url = Const.DOMAIN + item.getImgThird();
		// Tools.setImg(url, holder.rightBottomImg, context);
		// }
		//
		// }else{
		// holder.rightBottomImg.setVisibility(View.GONE);
		// }
		//
		// } else if ( getItemViewType(position) == THIRD){
		// holder.imgLayoutOnly.setVisibility(View.GONE);
		// holder.imgLayoutRight.setVisibility(View.GONE);
		// holder.imgLayoutLeft.setVisibility(View.VISIBLE);
		// if (item.getImgFrist() != null && !item.getImgFrist().equals("")) {
		// holder.leftRightImg.setVisibility(View.VISIBLE);
		// if(isLoadDB){
		// Tools.setDBImg(item.getImgFrist(), holder.leftRightImg);
		// }else{
		//
		// final String url = Const.DOMAIN + item.getImgFrist();
		// Tools.setImg(url, holder.leftRightImg, context);
		// }
		// } else {
		// holder.leftRightImg.setVisibility(View.GONE);
		// }
		//
		// if (item.getImgSecend() != null && !item.getImgSecend().equals("")) {
		// holder.leftTopImg.setVisibility(View.VISIBLE);
		// if(isLoadDB){
		// Tools.setDBImg(item.getImgSecend(), holder.leftTopImg);
		// }else{
		// final String url = Const.DOMAIN + item.getImgSecend();
		// Tools.setImg(url, holder.leftTopImg, context);
		// }
		// } else {
		// holder.leftTopImg.setVisibility(View.GONE);
		// }
		// if (item.getImgThird() != null && !item.getImgThird().equals("")) {
		// holder.leftBottomImg.setVisibility(View.VISIBLE);
		// if(isLoadDB){
		// Tools.setDBImg(item.getImgThird(), holder.leftBottomImg);
		// }else{
		// final String url = Const.DOMAIN + item.getImgThird();
		// Tools.setImg(url, holder.leftBottomImg, context);
		// }
		// }else{
		// holder.leftBottomImg.setVisibility(View.GONE);
		// }
		//
		// }

		// holder.userName.setText(comment.getUserName());
		// holder.time.setText(comment.getTime());

		return convertView;
	}

	static class ViewHolder {
		TextView title;
		TextView imgCount;
		TextView commentCount;
		ImageView onlyImg;
		ImageView rightLeftImg;
		ImageView rightTopImg;
		ImageView rightBottomImg;
		ImageView leftRightImg;
		ImageView leftTopImg;
		ImageView leftBottomImg;
		TextView time;
		LinearLayout imgLayoutOnly;
		LinearLayout imgLayoutRight;
		LinearLayout imgLayoutLeft;
	}

}
